package net.falcon;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.falcon.data.Arena;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.util.Vector;

public class Util {
	
	
	public static void loadFromConfig() {
		Configuration c = FalconArena.instance.getConfig();
		ConfigurationSection s = c.getConfigurationSection("arenas");
		if(s == null) { //no arenas
			return;
		}
		Set<String> maps = s.getKeys(false);
		for(String name : maps) {
			Arena a = new Arena();
			try {
			a.setName(name);
			a.setPt1((Vector) c.get("arenas." + name + ".pt1"));
			a.setPt2((Vector) c.get("arenas." + name + ".pt2"));
			a.setSpawns((ArrayList<Vector>) c.get("arenas." + name + ".spawns"));
			a.setSpawnInv((List<ItemStack>)c.get("arenas." + name + ".inventory"));
			a.setLives((Integer)c.get("arenas." + name + ".lives"));
			} catch(Exception e) {
				FalconArena.instance.getLogger().warning("Map '" + name + "' had bad data and did not load properly.");
				continue;
			}
			FalconArena.instance.addArena(a);
		}
	}
	
	public static Arena lookupArena(String name) {
		for(Arena a : FalconArena.instance.arenas) {
			if(a.getName().equals(name)) {
				return a;
			}
		}
		return null;
	}
	
	public static void saveInv() {
		
	}
	
	public static void saveMaps() {
		FileConfiguration c = FalconArena.instance.getConfig();
		for(Arena a : FalconArena.instance.arenas) {
			String basePath = "arenas." + a.getName();
			c.set(basePath + ".pt1", a.getPt1());
			c.set(basePath + ".pt2", a.getPt2());
			c.set(basePath + ".spawns", a.getSpawns());
			c.set(basePath + ".inventory", a.getSpawnInv());
			c.set(basePath + ".lives", a.getLives());
		}
		FalconArena.instance.saveConfig();
	}
	
	public static ArrayList<Vector> getTempSpawns(Player origin) {
		return ((ArrayList<Vector>)Util.getMetadata(origin, "tempArena.spawns"));
	}
	
	public static void print(String ln) {
		FalconArena.instance.getLogger().warning(ln);
	}
	
	public static Arena getArena(Player p) {
		return (Arena)Util.getMetadata(p, "fa.map");
	}
	
	public static boolean isInGame(Player p) {
		return Util.getMetadata(p, "fa.map")==null?false:true;
	}
	
	public static void sendMessage(Player p, String msg) {
		p.sendMessage(ChatColor.AQUA + "[FA] " + ChatColor.WHITE + msg);
	}
	
	public static Object getMetadata(Player p, String path) {
		List<MetadataValue> data = p.getMetadata(path);
		if(data.size() > 1) {
			print("Attempt to get metadata value '" + path + "' returned multiple values");
			return null;
		}
		if(data.size() < 1) {
			return null;
		}
		return ((FixedMetadataValue)data.get(0)).value();
	}
	
	public static void setMetadata(Player p, String path, Object o) {
		p.setMetadata(path, new FixedMetadataValue(FalconArena.instance, o));
	}
}
